







var workflowSelectedStep = null;

var workflowCssName = "";

var stepImageOpen = new Image();
stepImageOpen.src = "/_imgs/grid/r.gif";
var stepImageClose = new Image();
stepImageClose.src = "/_imgs/grid/d.gif";


var WaitTimeoutStepName = "WaitTimeout";
var WaitStepName = "WaitStep";
var WaitBranchStepName = "WaitBranch";

var ConditionStepName = "ConditionStep";
var IfConditionName = "IfCondition";
var ElseIfCondition = "ElseIfCondition";
var ElseCondition="ElseCondition";


var _expandCollapseArray = new Array();

function getElementWithStepname(el, stepName)
{
while (el != null)
{
if ((el.tagName.toLowerCase() == "table") && (el["stepname"] != undefined))
{

if ((stepName == "") || ((stepName != "") && (el.getAttribute("stepname") == stepName)))
{
return el;
}
else
{
el = el.parentNode;
}
}
else
{
el = el.parentNode;
}
}

return el;
}

function getElementWithStepnameAttribute(el)
{
return getElementWithStepname (el, "");
}

function OnWorkflowStepClick()
{
var el = event.srcElement;

el = getElementWithStepnameAttribute(el);

if( el == null )
{
return;
}

if( workflowSelectedStep != null )
{
UnselectStep( workflowSelectedStep );
}

SelectStep( el );
event.cancelBubble = true;
}








function GetAvailableCreateByEntityForStep()
{
var el = GetSelectedWorkflowStep();

var command;
if( el == null || el.id == "NoStep" )
{
command = new RemoteCommand("Workflow", "GetValidStepControlType");
if( el != null )
{
command.SetParameter("parentId", el.getAttribute( "parent" ) );
}
else
{
command.SetParameter( "parentId", "" );
}
command.SetParameter("siblingId", "" );
command.SetParameter("entityId", crmFormSubmit.crmFormSubmitId.value);
}
else
{
command = new RemoteCommand("Workflow", "GetValidStepControlType");
command.SetParameter("parentId", "");
command.SetParameter("siblingId", el.id );
command.SetParameter("entityId", crmFormSubmit.crmFormSubmitId.value);
}
var oResult = command.Execute();
if( oResult.Success )
{
return oResult.ReturnValue;
}

return " ";
}

function GetSelectedWorkflowStep()
{
return workflowSelectedStep;
}

function ResetSelectedStep()
{
if( workflowSelectedStep != null )
{
UnselectStep( workflowSelectedStep );
workflowSelectedStep = null;
workflowCssName = "";
}
}

function UnselectStep( el )
{
if ( el == null )
{
return;
}


el.className = workflowCssName;
}

function SelectStep( el )
{
if ( el == null )
{
return;
}

workflowSelectedStep = el;
workflowCssName = el.className;

el.className = "ms-crm-workflow-outer-selected";



try{
EnableDisableDeleteBtn();
}catch(e){

}
}

function GetConditionStep(selectedStep)
{

return getElementWithStepname(selectedStep, ConditionStepName);
}

function IsThereElseBranchExistInTheSelected( )
{
var el = GetSelectedWorkflowStep();


el = GetConditionStep(el);

if( el == null || el.firstChild == null )
{
return false;
}

var children = GetConditionStepChildren(el);

if (children[2] > 0)
{
return true;
}

return false;
}

function IsElseBranchSelected()
{
var el = GetSelectedWorkflowStep();

if ((el != null) && (el.getAttribute("Id") != "NoStep"))
{
var stepName = el.getAttribute("stepname");
if (stepName == "ElseCondition")
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}


function GetSelectedStepName()
{
var el = GetSelectedWorkflowStep();

if(el==null)
{
return null;
}

if ((el.getAttribute("Id") != "NoStep"))
{
var stepName = el.getAttribute("stepname");
return stepName;
}

return null;
}

function IsStageStepSelected()
{
var el = GetSelectedWorkflowStep();

if ((el != null) && (el.id.indexOf("StageStep") >= 0))
{
return true;
}

return false;
}

function IsIfElseBranchSelected()
{
var el = GetSelectedWorkflowStep();

if ((el != null) && (el.getAttribute("Id") != "NoStep"))
{
var stepName = el.getAttribute("stepname");
if ((stepName == "IfCondition") ||(stepName == "ElseIfCondition"))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}










function IsLastIfBranchWithElseSelected()
{
var el = GetSelectedWorkflowStep();

if ((el != null) && (el.getAttribute("stepname") == "IfCondition"))
{

el = GetConditionStep(el);

if( el == null || el.firstChild == null )
{
return false;
}


var children = GetConditionStepChildren(el);




if (children[1] == 0 && children[2] == 1)
{
return true;
}
}

return false;
}

function GetConditionStepChildren(conditionStep)
{
var children = new Array();
children[0] = 0;
children[1] = 0;
children[2] = 0;


var childNodes = conditionStep.getElementsByTagName("table");
for (var index = 0; index < childNodes.length; index++)
{
var oNode = childNodes[index];

if(!IsSiblingConditionalStep(conditionStep, oNode))
{
continue;
}

if (oNode.getAttribute("stepname") == "ElseIfCondition")
{
children[1]++;
}
else if (oNode.getAttribute("stepname") == "ElseCondition")
{
children[2]++;
}
else if (oNode.getAttribute("stepname") == "IfCondition")
{
children[0]++;
}
}

return children;
}

function IsSiblingConditionalStep(step1, child)
{

return GetDirectConditionStep(step1) ==  GetDirectConditionStep(child);
}

function GetDirectConditionStep(child)
{
while(!IsNull(child))
{
var attr = child.getAttribute("stepname");
if(attr == ConditionStepName)
{
return child;
}
child = child.parentElement;
}
return null;
}

function SelectedStepIsIfBranchWithNoSibling ()
{
var selectedStep = GetSelectedWorkflowStep();

if (selectedStep.getAttribute("stepname") == "IfCondition")
{
var parentStep = GetConditionStep(selectedStep);

var children = GetConditionStepChildren(parentStep);

if (children[0] == 1 &&
children[1] == 0 &&
children[2] == 0)
{
return true;
}
}

return false;
}


function SelectWorkflowStep( id )
{
if( id == null || id.length == 0 || id.toLowerCase() == "nostep" )
{
return;
}

var el = document.getElementById( id );

if( el != null )
{
SelectStep( el );
}
}





function ConfigureFormEntity(workflowId,stepId,primaryEntityName,mode)
{
event.cancelBubble = true;

var el = event.srcElement.nextSibling;

if( el == null )
{
return;
}


var arrTokens = el.id.split( "_" );




var entityNameValue = GetEntityNameValue(arrTokens[0],mode);

if(IsNull(entityNameValue))
{
return;
}

var entityName =  entityNameValue;
var relatedAttributeName = null;

if(entityName.indexOf(".")>0)
{
var nameArray = entityName.split('.');
if (nameArray.length == 3)
{

entityName = nameArray[0];
}
else
{
relatedAttributeName = nameArray[1];
entityName = nameArray[0];
}
}


if (mode == "3")
{
primaryEntityName = entityName;
entityName = "email";
}






var param;
if((el.value.length > 0) && (el.value == entityName))
{
param = "workflowId=" + CrmEncodeDecode.CrmUrlEncode( workflowId );
param += "&entityname=" + CrmEncodeDecode.CrmUrlEncode(entityName );
param += "&activityname=" + CrmEncodeDecode.CrmUrlEncode( arrTokens[0] );
}
else
{

param = "entityname=" + CrmEncodeDecode.CrmUrlEncode( entityName );
param += "&workflowId=" + CrmEncodeDecode.CrmUrlEncode( workflowId );
}


param += "&stepId=" + CrmEncodeDecode.CrmUrlEncode( arrTokens[0] );
param += "&entityFullName=" + entityNameValue;

if (!IsNull(primaryEntityName))
{
param += "&primaryentity=" + CrmEncodeDecode.CrmUrlEncode(primaryEntityName);
}

if (!IsNull(relatedAttributeName))
{
param += "&relatedattributename=" + CrmEncodeDecode.CrmUrlEncode(relatedAttributeName);
}




var readonlyMode = GetReadonlyMode(arrTokens[0],mode);
if(readonlyMode)
{
param += "&readonlymode=" + CrmEncodeDecode.CrmUrlEncode ("true");
}

if(!IsNull(mode))
{
param += "&mode="+mode;
}

if(mode == "3")
{
param += "&templateId=" + stepId;
}



var oWindowInfo = GetWindowInformation(Workflow);
var iDialogX	= oWindowInfo.Width;
var iDialogY	= oWindowInfo.Height;

if (entityName == "annotation")
{
oWindowInfo = GetWindowInformation(Annotation);
iDialogX	= oWindowInfo.Width + 220;
iDialogY	= oWindowInfo.Height + 300;

var dialogUrl = "/"+oWindowInfo.Url+"?stepId="+stepId+"&pId=" + CrmEncodeDecode.CrmUrlEncode( workflowId ) +"&pType=4703";


var command = new RemoteCommand("Workflow", "GetWorkflowStepNoteId");
command.SetParameter("activityId", stepId );
command.SetParameter("entityId", workflowId);
var oResult = command.Execute();
if( oResult.Success && oResult.ReturnValue.length > 0)
{
dialogUrl += "&id="+CrmEncodeDecode.CrmUrlEncode(oResult.ReturnValue);
}
if(readonlyMode)
{
dialogUrl += "&readonlymode=" + CrmEncodeDecode.CrmUrlEncode ("true");
}
openStdWin(prependOrgName(dialogUrl), iDialogX, iDialogY);
if(!readonlyMode)
{
UpdateStepDescription();
}
}
else
{
var sRetVal = openStdDlg(prependOrgName("/SFA/Workflow/entityform.aspx?" + param), new Object(), iDialogX, iDialogY+20,true);

if( sRetVal != null && sRetVal.length > 0 )
{
SetStepConfiguration(arrTokens[0], entityNameValue,sRetVal,mode);
}
}
}

function GetEntityNameValue(stepName, mode)
{
if(mode == "2")
{
return "email";
}

var elLst = document.getElementById( stepName + "_entitylstwfc" );

if (elLst.DataValue != undefined)
{
return elLst.DataValue;
}
else
{
return elLst.value;
}
}

function GetReadonlyMode(stepName, mode)
{
var elLst;
if(mode == "2")
{
elLst = document.getElementById( stepName + "_sendemailfromwfc" );
}
else
{
elLst = document.getElementById( stepName + "_entitylstwfc" );
}
return elLst.disabled;
}

function WorkflowEntityLstChange(mode)
{




var el = event.srcElement;


var arrTokens = el.id.split( "_" );


var elInput = document.getElementById( arrTokens[0] + "_activitystatewfc" );

if( elInput.value.length > 0 )
{
if( confirm( LOCID_CONFIRM_CHANGE_ENTITY ) )
{

elInput.value = "";
CleanUpAttachment(crmFormSubmit.crmFormSubmitId.value, arrTokens[0]);
}
else
{

el.DataValue = el.DefaultValue;
return false;
}
}

var entityName =  el.DataValue;
if(entityName.indexOf(".")>0)
{
entityName = entityName.split(".")[0];
}
var config = "<" + entityName + "/>";


SetStepConfiguration(arrTokens[0], el.DataValue, config, mode);

return true;
}








function SetStepConfiguration(stepId, entity, formConfig, stepMode )
{

var sb = new StringBuilder();
sb.Append( '<and>' );

if(stepMode == "3")// "3" for email template
{
var eType = document.getElementById(stepId + "_sendemailfromwfc" );
var eTypeValue;
if (eType.DataValue != undefined)
{
eTypeValue = eType.DataValue;
}
else
{
eTypeValue = eType.value;
}

sb.Append( '<condition><column id="colAction" value="sendemail"></column>' );
sb.Append( '<column id="colType" value="' + eTypeValue + '"></column>' );
if(formConfig.indexOf("<templateid>")>0)
{
var templateId = formConfig.substring(formConfig.indexOf("<templateid>"));
formConfig = formConfig.substring(0,formConfig.indexOf("<templateid>"));
templateId = templateId.substring(templateId.indexOf(">") + 1, templateId.indexOf("</templateid>"));
sb.Append( '<column id="colTemplate" value="' + templateId + '"></column>' );
}
}
else
if(stepMode == "2")// "2" for sendemail
{
var eType = document.getElementById(stepId + "_sendemailfromwfc" );

var eTypeValue;
if (eType.DataValue != undefined)
{
eTypeValue = eType.DataValue;
}
else
{
eTypeValue = eType.value;
}

sb.Append( '<condition><column id="colAction" value="sendemail"></column>' );
sb.Append( '<column id="colType" value="' + eTypeValue + '"></column>' );
}
else
{
if(stepMode == "1")// "1" for update
{

sb.Append( '<condition><column id="colAction" value="update"></column>' );
}
else
{
sb.Append( '<condition><column id="colAction" value="create"></column>' );
}
}

sb.Append( '<column id="colParameter" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode(entity) + '"></column>' );
sb.Append( '<column id="colConfiguration" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode(formConfig) + '"></column>' );
sb.Append( '</condition>' );
sb.Append( '</and>' );

if(stepMode=="2" || stepMode=="3") 
{
setSendEmailStep(stepId, sb.ToString());
}
else
{
if(stepMode=="1") 
{
setUpdateEntityStep(stepId, sb.ToString());
}
else
{
setCreateEntityStep(stepId, sb.ToString());
}
}
}




function OnSelectAssigneeReturn()
{
var el = event.srcElement;
UpdateAssigneeForControl(el);
}

function UpdateAssigneeForControl(el)
{

var arrTokens = el.id.split( "_" );


var elLst = document.getElementById( arrTokens[0] + "_entitylstwfc" );


if (IsNull(el.DataValue))
{
UpdateAssignStep(arrTokens[0], elLst.DataValue, "");
}
else
{
UpdateAssignStep(arrTokens[0], elLst.DataValue, el.DataXml);
}
}


function OnAssignStepEntityLstChange()
{
var el = event.srcElement;


var arrTokens = el.id.split( "_" );

var elLu = document.getElementById( arrTokens[0] + "_lookupwfc" );

if( elLu != null )
{
elLu.Clear();
}
UpdateAssignStep(arrTokens[0], el.DataValue, "");

}


function UpdateAssignStep(stepId,entityName, dataXml)
{
var sb = new StringBuilder();
sb.Append( '<and>' );
sb.Append( '<condition><column id="colAction" value="assign"></column>' );
sb.Append( '<column id="colParameter" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( entityName ) + '"></column>' );
sb.Append( '<column id="colConfiguration" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( dataXml ) + '"></column>' );
sb.Append( '</condition>' );
sb.Append( '</and>' );
setAssignEntityStep( stepId, sb.ToString() );
}




function OnSendEmailFromChanged()
{
var el = event.srcElement;


var arrTokens = el.id.split( "_" );

var elInput = document.getElementById( arrTokens[0] + "_activitystatewfc" );

if( elInput.value.length > 0 )
{
if( confirm( LOCID_CONFIRM_CHANGE_ENTITY ) )
{

elInput.value = "";
CleanUpAttachment(crmFormSubmit.crmFormSubmitId.value, arrTokens[0]);
}
else
{

el.DataValue = el.DefaultValue;
return false;
}
}

var sb = new StringBuilder();
sb.Append( '<and>' );

sb.Append( '<condition><column id="colAction" value="sendemail"></column>' );
sb.Append( '<column id="colType" value="');
sb.Append( el.DataValue );
sb.Append( '"></column>' );
sb.Append( '<column id="colParameter" value=""></column>' );
sb.Append( '<column id="colConfiguration" value=""></column>' );
sb.Append( '</condition>' );
sb.Append( '</and>' );
setSendEmailStep(arrTokens[0], sb.ToString());

return true;
}


function CleanUpAttachment(workflowId, stepId)
{
if( IsNull(workflowId)|| IsNull(stepId) )
{
return
}


var command;
command = new RemoteCommand( "Workflow", "DeleteAttachmentStep" );
command.SetParameter( "workflowId", workflowId );
command.SetParameter( "stepId", stepId );
command.Execute();
}




function OnSelectChildWorkflowReturn()
{
var el = event.srcElement;


var arrTokens = el.id.split( "_" );


var elLst = document.getElementById( arrTokens[0] + "_entitylstwfc" );

UpdateChildworkflowStep(arrTokens[0], elLst.DataValue, el.DataXml);
}


function OnChildWorkflowEntityChanged()
{
var elist = event.srcElement;


var arrTokens = elist.id.split( "_" );

var elLu = document.getElementById( arrTokens[0] + "_lookupwfc" );

if( elLu != null )
{
elLu.Clear();
}

UpdateChildworkflowStep(arrTokens[0], elist.DataValue, "");
}

function UpdateChildworkflowStep(stepId, entityName, dataXml)
{
var sb = new StringBuilder();
sb.Append( '<and>' );
sb.Append( '<condition><column id="colAction" value="childworkflow"></column>' );
sb.Append( '<column id="colParameter" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( entityName ) + '"></column>' );
sb.Append( '<column id="colConfiguration" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode(dataXml) + '"></column>' );
sb.Append( '</condition>' );
sb.Append( '</and>' );
setChildWorkflowStep(stepId, sb.ToString() );
}



function ConfigureAssignTarget(workflowId)
{
event.cancelBubble = true;


var el = event.srcElement.nextSibling;

var arrTokens = el.id.split( "_" );

var param;

param = "workflowId=" + CrmEncodeDecode.CrmUrlEncode( workflowId );
param += "&activityname=" + CrmEncodeDecode.CrmUrlEncode( arrTokens[0] );
param += "&readonlymode=" + CrmEncodeDecode.CrmUrlEncode (event.srcElement.readonly);

var sRetVal = openStdDlg(prependOrgName("/SFA/Workflow/assignactivityform.aspx?" +param), new Object(), 850, 600);

if( sRetVal != null && sRetVal.length > 0 )
{
var sb = new StringBuilder();

sb.Append( '<and>' );
sb.Append( '<condition><column id="colAction" value="assign"></column>' );
sb.Append( '<column id="colParameter" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( el.value  ) + '"></column>' );
sb.Append( '<column id="colConfiguration" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( sRetVal ) + '"></column>' );
sb.Append( '</condition>' );
sb.Append( '</and>' );


var elLst = document.getElementById( arrTokens[0] + "_entitylstwfc" );







UpdateAssignStep(arrTokens[0], elLst.DataValue, sRetVal);
}
}




function ConfigureCustomActivity(workflowId)
{
event.cancelBubble = true;


var el = event.srcElement.nextSibling;

var arrTokens = el.id.split( "_" );

var param;

param = "workflowId=" + CrmEncodeDecode.CrmUrlEncode( workflowId );
param += "&activityname=" + CrmEncodeDecode.CrmUrlEncode( arrTokens[0] );
param += "&readonlymode=" + CrmEncodeDecode.CrmUrlEncode (event.srcElement.readonly);

var sRetVal = openStdDlg(prependOrgName("/SFA/Workflow/customactivityform.aspx?" +param), new Object(), 850, 600);

if( sRetVal != null && sRetVal.length > 0 )
{
var sb = new StringBuilder();

sb.Append( '<and>' );
sb.Append( '<condition><column id="colAction" value="customActivity"></column>' );
sb.Append( '<column id="colParameter" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( el.value  ) + '"></column>' );
sb.Append( '<column id="colConfiguration" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( sRetVal ) + '"></column>' );
sb.Append( '</condition>' );
sb.Append( '</and>' );

setCustomActivityStep( arrTokens[0], sb.ToString() );
}
}





function OnChangeStatusStepStateChanged()
{
var el = event.srcElement;

var arrTokens = el.id.split( "_" );


var elLst = document.getElementById( arrTokens[0] + "_entitylstwfc" );
var stLst = document.getElementById( arrTokens[0] + "_statuslstwfc" );
var sb = new StringBuilder();
sb.Append( '<and>' );
sb.Append( '<condition><column id="colAction" value="changeStatus"></column>' );
sb.Append( '<column id="colParameter" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( elLst.DataValue ) + '"></column>' );
sb.Append( '<column id="colStatus" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( stLst.DataValue ) + '"></column>' );
sb.Append( '<column id="colState" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( StatusToState(stLst)) + '"></column>' );
sb.Append( '</condition>' );
sb.Append( '</and>' );
setChangeStatusStep( arrTokens[0], sb.ToString() );
}

function StatusToState(stLst)
{

var oGroups = stLst.children;
for (var i=0; i < oGroups.length; i++)
{
oGroup = oGroups.item(i);
for (var j=0;j < oGroup.children.length; j++)
{
if (oGroup.children.item(j).getAttribute("value") == stLst.DataValue)
{
return oGroup.getAttribute("id");
}
}
}
}

function OnChangeStatusStepEntityLstChanged()
{

var elLst = event.srcElement;
var arrTokens = elLst.id.split( "_" );

var stLst = document.getElementById( arrTokens[0] + "_statuslstwfc" );
var stateVal;

for(var i=0;i<elLst.all.length;i++)
{
var el = elLst.all[i];
if(el.tagName == "OPTION" && el.value == elLst.DataValue)
{
stateVal = elLst.all[i].state;
break;
}
}

var val = CrmEncodeDecode.CrmXmlDecode(stateVal);
if(val.indexOf("<")==-1)
{
val = CrmEncodeDecode.CrmXmlDecode(val);
}

var oXml = loadXmlDocument(val);

for ( var i = 0; i < stLst.childNodes.length;)
{
stLst.childNodes[i].removeNode( true );
}
var oNodes = oXml.selectNodes("/states/state");
var iSize = oNodes.length;
for( var i=0; i < iSize; i++)
{
var oNode = oNodes.item(i);
var groupId = oNode.getAttribute("value");
stLst.AddOptionGroup(oNode.getAttribute("label"),groupId , "asending");

var statusNodes = oNode.childNodes ;
for(var j=0;j<statusNodes.length;j++)
{
var statusNode = statusNodes.item(j);
stLst.AddOption(CrmEncodeDecode.CrmXmlDecode(statusNode.getAttribute("label")), statusNode.getAttribute("value"), groupId)
}
}
stLst.selectedIndex=0;
stLst.fireEvent("onchange");

}




function OnChangeStopWorkflowLstChanged()
{
var el = event.srcElement;
var arrTokens = el.id.split( "_" );

var sb = new StringBuilder();
sb.Append( '<and>' );
sb.Append( '<condition><column id="colAction" value="stopWorkflow"></column>' );
sb.Append( '<column id="colParameter" value="' );
sb.Append( CrmEncodeDecode.CrmXmlEncode( el.DataValue ) + '"></column>' );
sb.Append( '</condition>' );
sb.Append( '</and>' );
setStopWorkflowStep( arrTokens[0], sb.ToString() );
}



var WorkflowStepDescriptionCache = new Array();




function OnWFStepDescriptionChange()
{
var el = event.srcElement;

var elValue = el.value;
var defaultValue = el.getAttribute( "DefaultValue" );


if( elValue.length != defaultValue.length || elValue != defaultValue )
{

var arrTokens = el.id.split( "_" );
WorkflowStepDescriptionCache[arrTokens[0]] = elValue;
}
}


function GetWFStepDescriptionXml()
{
var isThereChange = false;
var sb = new StringBuilder();
sb.Append( "<steps>" );
var stepName;
for( stepName in WorkflowStepDescriptionCache )
{
sb.Append( "<step><name>" );
sb.Append( CrmEncodeDecode.CrmXmlEncode( stepName ) );
sb.Append( "</name>" );
sb.Append( "<description>" );
sb.Append( CrmEncodeDecode.CrmXmlEncode( WorkflowStepDescriptionCache[stepName] ) );
sb.Append( "</description>" );
sb.Append( "</step>" );

isThereChange = true;
}
sb.Append( "</steps>" );


if( !isThereChange )
{
return "";
}
else
{
return sb.ToString();
}
}


function IsWFDescriptionModified()
{

for( stepName in WorkflowStepDescriptionCache )
{
return true;
}
return false;
}

function ResetWFStepDescriptionArray()
{
WorkflowStepDescriptionCache = new Array();
}

function ExpandCollapseStep(divId, imgId)
{
var element = document.getElementById(divId);

if (element.style.display == 'block')
{
element.style.display = "none";
document[imgId].src = stepImageOpen.src;
_expandCollapseArray[divId] = imgId;
}
else
{
element.style.display = "block";
document[imgId].src = stepImageClose.src;
_expandCollapseArray[divId] = null;
}
}

function RecollapseSteps()
{
var divId;
for (divId in _expandCollapseArray)
{
var imgId = _expandCollapseArray[divId];
if (imgId != null)
{
ExpandCollapseStep(divId, imgId);
}
}
}


function IsDataSlugSpecified(dataControl)
{
var control = GetValueControlWithSlug(dataControl);
if(control.IsDataSlug != "undefined")
{
return control.IsDataSlug;
}
return false;
}


function SaveAndCloseCustomStepConfiguration()
{

var sb = new StringBuilder();

var i = 0, index = 0;
sb.Append('<parameters>');

var parameterControl = document.getElementById("ParameterValue"+i);

while(parameterControl!=null)
{

sb.Append('<parameter name="'+parameterControl.parametername+'">');


var isRequired = false;

if(parameterControl.required == "True")
{
isRequired = true;
}

if (IsDataSlugSpecified(parameterControl))
{
sb.Append("<slugbody>");
sb.Append(GetValueControlWithSlug(parameterControl).FirstSlugValue);
sb.Append("</slugbody>");
}
else
{

switch(parameterControl.parameterType)
{


case "CrmBoolean":
{

var boolValue = null;
boolValue =parameterControl.DataValue;
if(boolValue != null)
{
sb.Append(boolValue);
}
else if( isRequired )
{
alert(formatString(LOCID_MSG_VALUEREQUIRED, parameterControl.parametername));
return;
}
}
break;
case "CrmDateTime":


if( isRequired && (parameterControl.all["DateInput"].value == null || parameterControl.all["DateInput"].value == ""))
{
alert(formatString(LOCID_MSG_VALUEREQUIRED, parameterControl.parametername));
return;
}

if(!IsNull(parameterControl.DataValue))
{
var oXml =loadXmlDocument(parameterControl.DataXml);
var oNodes = oXml.selectNodes("/");
if(oNodes.length==1)
{
sb.Append(oNodes[0].text);
}
}

break;

case "Lookup":
case "Owner":
case "Customer":

if(parameterControl.parentElement.parentElement.all[2].tagName=="SPAN")
{
sb.Append('<refId>'+parameterControl.parentElement.parentElement.all[2].oid+'</refId>');
sb.Append('<refType>'+parameterControl.parentElement.parentElement.all[2].otype+'</refType>');
sb.Append('<refLabel>'+CrmEncodeDecode.CrmXmlEncode(parameterControl.parentElement.parentElement.all[2].innerText)+'</refLabel>');
}
else
{

if(isRequired)
{
alert(formatString(LOCID_MSG_VALUEREQUIRED, parameterControl.parametername));
return;
}
}

break;

default:


if( isRequired && (parameterControl.value == null || parameterControl.value ==""))
{
alert(formatString(LOCID_MSG_VALUEREQUIRED, parameterControl.parametername));
return;
}

sb.Append(CrmEncodeDecode.CrmXmlEncode(parameterControl.DataValue));
break;
}
}
sb.Append('</parameter>');

i++;
parameterControl = document.getElementById("ParameterValue"+i);
}

sb.Append('</parameters>');
window.returnValue = sb.ToString();
window.close();
}

function SetFieldFocusAtOnLoad(activeElement)
{
if(!IsNull(activeElement))
{
var element = activeElement;

while(!IsNull(element.parentElement))
{
if(!IsNull(element.id) && element.id!="")
{
break;
}
element = element.parentElement;
}


if( activeElement.className == "ms-crm-Lookup" )
{
element = element.getElementsByTagName("IMG");
if(!IsNull(element) && element.length > 0)
{
SetFieldFocus(element[0]);
}
}
else if(activeElement.className == "ms-crm-DateTime" )
{
element = element.getElementsByTagName("INPUT");
if(!IsNull(element) && element.length > 0)
{
SetFieldFocus(element[0]);
}
}
else
{
SetFieldFocus(element);
}
}
}

function SetFieldFocus(ele)
{
if(ele.style.display!="none" && ele.style.visibility!="hidden" && !ele.isDisabled)
{
ele.fireEvent("onfocus"); ;
}
}




function GetHiddenDataTypeMappingInfo(dataType)
{
try
{
var command;

if(!IsWorkflowEditForm())
{
return null;
}

command = new RemoteCommand("Workflow", "GetDataTypeMappingInfo");
command.SetParameter("workflowId", crmFormSubmit.crmFormSubmitId.value);
command.SetParameter("dataType", dataType);
var oResult = command.Execute();
if( oResult.Success )
{
return oResult.ReturnValue;
}
}
catch(e)
{
}
return null;
}

function GetEntityListMappingInfo(dataType)
{
try
{
var command;
if(!IsWorkflowEditForm())
{
return null;
}

command = new RemoteCommand("Workflow", "GetEntityListHtml");
command.SetParameter("workflowId", crmFormSubmit.crmFormSubmitId.value);
command.SetParameter("dataType", dataType);
var oResult = command.Execute();
if( oResult.Success )
{
return oResult.ReturnValue;
}
}
catch(e)
{
}
return null;
}

function IsWorkflowEditForm()
{
var stepArea =  document.getElementById("gridDefinitionContainer");
if(IsNull(stepArea) || stepArea.className != 'wfWorkflowStepArea')
{
return false;
}

return true;
}

